package com.angel.webapp.servlets;

import java.io.IOException;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.fileupload.FileItem;

import com.angel.architecture.exceptions.NonBusinessException;
import com.angel.common.keyValue.KeyValueResult;
import com.angel.io.descriptor.ExportedFileDescriptor;

/**
 * @author William
 * @since 03/February/2009
 */
public class UploadEmailAttachmentServlet extends UploadServlet{

    protected static final String HEADER_KEY_CACHE_CONTROL = "Cache-Control";

    protected static final String HEADER_KEY_CONTENT_DISPOSITION = "Content-Disposition";

    protected static final String CONTENT_TYPE_NAME = "Cache-Control";

    protected static final String HEADER_VALUE_MAX_AGE = "max-age=" + (60 * 60 * 24);

    protected static final String HEADER_VALUE_ATTACHMENT= "attachment;filename=";

    protected static final String REQ_PARAMETER_FILE_PROCESSOR_NAME = "fileProcessorName";

    protected static final String REQ_PARAMETER_SEARCH_STRATEGY_NAME = "searchStrategyName";

    protected static final String REQ_PARAMETER_ROW_PROCESSOR_NAME = "rowProcessorName";

    protected static final String REQ_PARAMETER_SEPARATOR_PARAMETERS = "separatorParameters";

    protected static final String REQ_PARAMETER_PARAMETERS_NAME = "searchParameters";

    protected static final String DEFAULT_SEPARATOR_PARAMETERS = "*";

    private static final long serialVersionUID = -7427467996390884097L;

    protected void doGet(final HttpServletRequest req, final HttpServletResponse res) throws IOException {
        this.doPost(req, res);
    }

    /** Check request parameters to can make export process. If parameters necessaries are not present in the request, it throws
     * a NonBusinessException.
     *
     * @param keyValueResult
     * @see com.angel.arquitectura.exception.NonBusinessException
     */
    protected void doCheckParameters(KeyValueResult keyValueResult) {
        boolean containsParameterFileProcessorName = keyValueResult.containsResultFor(REQ_PARAMETER_FILE_PROCESSOR_NAME);
        boolean containsParameterRowProcessorName = keyValueResult.containsResultFor(REQ_PARAMETER_ROW_PROCESSOR_NAME);
        boolean containsParameterSearchStrategyName = keyValueResult.containsResultFor(REQ_PARAMETER_SEARCH_STRATEGY_NAME);

        if(!containsParameterFileProcessorName || !containsParameterRowProcessorName || !containsParameterSearchStrategyName){
            String errorMessage = "Parameters necesary for export process are not present: " +
                    "Parameter Name: " + REQ_PARAMETER_FILE_PROCESSOR_NAME + " Included: " + containsParameterFileProcessorName +
                    "Parameter Name: " + REQ_PARAMETER_ROW_PROCESSOR_NAME + " Included: " + containsParameterRowProcessorName +
                    "Parameter Name: " + REQ_PARAMETER_SEARCH_STRATEGY_NAME + " Included: " + containsParameterSearchStrategyName;
            throw new NonBusinessException(errorMessage);
        }
    }

    protected void createOutputForResponse(ExportedFileDescriptor exportedFileDescriptor, HttpServletRequest request, HttpServletResponse response) {
       /* String fileName = exportedFileDescriptor.getFileName();
        String contentType = exportedFileDescriptor.getContentType();
        final InputStream in = exportedFileDescriptor.getInputStream();
        int length = exportedFileDescriptor.getLength();


        response.setHeader(HEADER_KEY_CACHE_CONTROL, HEADER_VALUE_MAX_AGE);
        response.setContentType(contentType); //
        response.addHeader(HEADER_KEY_CONTENT_DISPOSITION, HEADER_VALUE_ATTACHMENT + fileName);

        if (length > 0)
            response.setContentLength(length);

        //final InputStream in = new ByteArrayInputStream(fileContent.getBytes());

        OutputStream out = null;
        try {
            out = response.getOutputStream();
            byte[] buf = new byte[1024];
            int count;
            while ((count = in.read(buf)) >= 0) {
                out.write(buf, 0, count);
            }
            out.flush();
        } catch (IOException e) {
            throw new NonBusinessException(e);
        } finally {
            try {
                if (in != null) {
                    in.close();
                }
                if (out != null) {
                    out.close();
                }
            } catch (IOException e) {

            }
        }*/
    }

	@Override
	protected void doProcess(FileItem fileItem, KeyValueResult keyValueResult, HttpServletRequest req, 
			HttpServletResponse res) {
		// TODO Auto-generated method stub
		
	}
}